<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '110px'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="耳号" prop="earmark">
            <el-input v-model="dataForm.earmark" placeholder="耳号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发病时间" prop="diseaseDate">
            <el-input v-model="dataForm.diseaseDate" placeholder="发病时间"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="症状描述" prop="desp">
            <el-input v-model="dataForm.desp" placeholder="症状描述"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="化验情况" prop="testing">
            <el-input v-model="dataForm.testing" placeholder="化验情况"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="诊断结果" prop="results">
            <el-input v-model="dataForm.results" placeholder="诊断结果"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="治疗日期" prop="cureDate">
            <el-input v-model="dataForm.cureDate" placeholder="治疗日期"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="治疗方案" prop="curePlan">
            <el-input v-model="dataForm.curePlan" placeholder="治疗方案"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="休药期(天)" prop="xiuyaoqi">
            <el-input v-model="dataForm.xiuyaoqi" placeholder="休药期(天)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="解除日期" prop="temiDate">
            <el-input v-model="dataForm.temiDate" placeholder="解除日期"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="治疗人员" prop="curePersion">
            <el-input v-model="dataForm.curePersion" placeholder="治疗人员"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="恢复情况" prop="recovery">
        <el-input v-model="dataForm.recovery" placeholder="恢复情况"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>/* eslint-disable */
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        earmark: '',
        diseaseDate: '',
        desp: '',
        testing: '',
        results: '',
        cureDate: '',
        curePlan: '',
        xiuyaoqi: '',
        temiDate: '',
        curePersion: '',
        recovery: '',
        remark: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        earmark: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        diseaseDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        desp: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        testing: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        results: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        cureDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        curePlan: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        xiuyaoqi: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        temiDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        curePersion: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        recovery: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/history/disease/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/history/disease/', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false})
  }
}
</script>
